mruby 4.0.0
mruby is the lightweight implementation of the Ruby language
Loading...
Searching...
No Matches
compile.h
Go to the documentation of this file.
1
6
7#ifndef MRUBY_COMPILE_H
8#define MRUBY_COMPILE_H
9
10#include "common.h"
11#include "mruby/mempool.h"
12
17
18#include <mruby.h>
19
20struct mrb_parser_state;
21/* load context */
22typedef struct mrb_ccontext {
23 mrb_sym *syms;
24 int slen;
25 char *filename;
26 uint16_t lineno;
27 int (*partial_hook)(struct mrb_parser_state*);
28 void *partial_data;
29 struct RClass *target_class;
30 mrb_bool capture_errors:1;
31 mrb_bool dump_result:1;
32 mrb_bool no_exec:1;
33 mrb_bool keep_lv:1;
34 mrb_bool no_optimize:1;
35 mrb_bool no_ext_ops:1;
36 mrb_bool no_return_value:1;
37 const struct RProc *upper;
38
39 size_t parser_nerr;
40} mrb_ccontext; /* compiler context */
41
42MRB_API mrb_ccontext* mrb_ccontext_new(mrb_state *mrb);
43MRB_API void mrb_ccontext_free(mrb_state *mrb, mrb_ccontext *cxt);
44MRB_API const char *mrb_ccontext_filename(mrb_state *mrb, mrb_ccontext *c, const char *s);
45MRB_API void mrb_ccontext_partial_hook(mrb_ccontext *c, int (*partial_hook)(struct mrb_parser_state*), void*data);
46MRB_API void mrb_ccontext_cleanup_local_variables(mrb_ccontext *c);
47
48/* compatibility macros */
49#define mrbc_context mrb_ccontext
50#define mrbc_context_new mrb_ccontext_new
51#define mrbc_context_free mrb_ccontext_free
52#define mrbc_filename mrb_ccontext_filename
53#define mrbc_partial_hook mrb_ccontext_partial_hook
54#define mrbc_cleanup_local_variables mrb_ccontext_cleanup_local_variables
55
56/* AST node structure */
57typedef struct mrb_ast_node mrb_ast_node;
58
59/* lexer states */
60enum mrb_lex_state_enum {
61 EXPR_BEG, /* ignore newline, +/- is a sign. */
62 EXPR_END, /* newline significant, +/- is an operator. */
63 EXPR_ENDARG, /* ditto, and unbound braces. */
64 EXPR_ENDFN, /* ditto, and unbound braces. */
65 EXPR_ARG, /* newline significant, +/- is an operator. */
66 EXPR_CMDARG, /* newline significant, +/- is an operator. */
67 EXPR_MID, /* newline significant, +/- is a sign. */
68 EXPR_FNAME, /* ignore newline, no reserved words. */
69 EXPR_DOT, /* right after '.' or '::', no reserved words. */
70 EXPR_CLASS, /* immediate after 'class', no here document. */
71 EXPR_VALUE, /* alike EXPR_BEG but label is disallowed. */
72 EXPR_MAX_STATE
73};
74
75/* saved error message */
77 uint16_t lineno;
78 int column;
79 char* message;
80};
81
82#define MRB_PARSER_TOKBUF_MAX (UINT16_MAX-1)
83#define MRB_PARSER_TOKBUF_SIZE 256
84
85/* parser structure */
87 mrb_state *mrb;
88 mempool *pool;
89 mrb_ast_node *cells;
90 const char *s, *send;
91#ifndef MRB_NO_STDIO
92 /* If both f and s are non-null, it will be taken preferentially from s until s < send. */
93 FILE *f;
94#endif
95 mrb_ccontext *cxt;
96 mrb_sym filename_sym;
97 uint16_t lineno;
98 int column;
99
100 enum mrb_lex_state_enum lstate;
101 struct parser_lex_strterm *lex_strterm;
102
103 unsigned int cond_stack;
104 unsigned int cmdarg_stack;
105 int paren_nest;
106 int lpar_beg;
107 int in_def, in_single, in_kwarg;
108 mrb_bool cmd_start:1;
109 mrb_ast_node *locals;
110
111 mrb_ast_node *pb;
112 char *tokbuf;
113 char buf[MRB_PARSER_TOKBUF_SIZE];
114 int tidx;
115 int tsiz;
116
117 mrb_ast_node *heredocs_from_nextline;
118 mrb_ast_node *parsing_heredoc;
119
120 void *ylval;
121
122 size_t nerr;
123 size_t nwarn;
124 mrb_ast_node *tree;
125
126 mrb_bool no_optimize:1;
127 mrb_bool capture_errors:1;
128 mrb_bool no_ext_ops:1;
129 mrb_bool no_return_value:1;
130 const struct RProc *upper;
131 struct mrb_parser_message error_buffer[10];
132 struct mrb_parser_message warn_buffer[10];
133
134 mrb_sym* filename_table;
135 uint16_t filename_table_length;
136 uint16_t current_filename_index;
137 uint16_t prev_file_lineno; /* saved lineno before partial_hook file switch */
138
139 /* Variable-sized node management */
140 mrb_ast_node *nvars;
141};
142
143MRB_API struct mrb_parser_state* mrb_parser_new(mrb_state*);
144MRB_API void mrb_parser_free(struct mrb_parser_state*);
145MRB_API void mrb_parser_parse(struct mrb_parser_state*,mrb_ccontext*);
146
147MRB_API void mrb_parser_set_filename(struct mrb_parser_state*, char const*);
148MRB_API mrb_sym mrb_parser_get_filename(struct mrb_parser_state*, uint16_t idx);
149
150/* utility functions */
151#ifndef MRB_NO_STDIO
152MRB_API struct mrb_parser_state* mrb_parse_file(mrb_state*,FILE*,mrb_ccontext*);
153#endif
154MRB_API struct mrb_parser_state* mrb_parse_string(mrb_state*,const char*,mrb_ccontext*);
155MRB_API struct mrb_parser_state* mrb_parse_nstring(mrb_state*,const char*,size_t,mrb_ccontext*);
156MRB_API struct RProc* mrb_generate_code(mrb_state*, struct mrb_parser_state*);
157MRB_API mrb_value mrb_load_exec(mrb_state *mrb, struct mrb_parser_state *p, mrb_ccontext *c);
158
175#ifndef MRB_NO_STDIO
177MRB_API mrb_value mrb_load_file_cxt(mrb_state*,FILE*, mrb_ccontext *cxt);
178MRB_API mrb_value mrb_load_detect_file_cxt(mrb_state *mrb, FILE *fp, mrb_ccontext *c);
179#endif
180MRB_API mrb_value mrb_load_string(mrb_state *mrb, const char *s);
181MRB_API mrb_value mrb_load_nstring(mrb_state *mrb, const char *s, size_t len);
182MRB_API mrb_value mrb_load_string_cxt(mrb_state *mrb, const char *s, mrb_ccontext *cxt);
183MRB_API mrb_value mrb_load_nstring_cxt(mrb_state *mrb, const char *s, size_t len, mrb_ccontext *cxt);
184
187
188#endif /* MRUBY_COMPILE_H */
mruby Symbol.
mruby common platform definition"
#define MRB_END_DECL
End declarations in C mode.
Definition common.h:28
#define MRB_BEGIN_DECL
Start declarations in C mode.
Definition common.h:26
#define MRB_API
Declare a public mruby API function.
Definition common.h:108
mrb_value mrb_load_file(mrb_state *, FILE *)
program load functions
Class class.
Definition class.h:17
Definition proc.h:53
Definition mempool.c:52
Definition compile.h:22
Definition compile.h:76
Definition compile.h:86
Definition mruby.h:280
Definition boxing_nan.h:40